{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73e99ef9",
   "metadata": {},
   "source": [
    "* 项目的核心知识点\n",
    "> 1. Flask角色定位:中央登记中心\n",
    ">> 1.路由:\n",
    ">>> 1. url 相对路径(不完整的路径):相对于https://127.0.0.1:5000的相对路径,   \n",
    ">>> 2.HTTP方法:【GET 默认】【POST 有用户输入】\n",
    ">> 2. 视图函数\n",
    ">>> 1. 书写函数的过程(if elif else for while etc.)处理数据\n",
    ">>> 2. return(返回值):text文本、HTML文件、JSON数据文件 etc.\n",
    "\n",
    "> 2. HTML模板渲染\n",
    ">> 1. Python文件 ---> HTML文件 --->render_template() ---> from flask import render_template()\n",
    ">> 2. 在 期末项目.ipynb 文件 同一个路径下新建一个templates文件夹专门存放页面文件\n",
    ">> 3. render_template('HTML 文件名')\n",
    "> 3. [from表单](https://www.w3school.com.cn/html/html_forms.asp)\n",
    ">> 1. 用于手机用户输入\n",
    ">> 2. input: text 输入框;submit提交按钮\n",
    ">> 3. 一般是POST请求，需要注意以下两点：\n",
    ">>> 1. method='POST'\n",
    ">>> 2. action='对应的页面路由'\n",
    "> 4. request.\n",
    ">> 1. request.from接受数据\n",
    "...\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "038cd8dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method route in module flask.app:\n",
      "\n",
      "route(rule, **options) method of flask.app.Flask instance\n",
      "    A decorator that is used to register a view function for a\n",
      "    given URL rule.  This does the same thing as :meth:`add_url_rule`\n",
      "    but is intended for decorator usage::\n",
      "    \n",
      "        @app.route('/')\n",
      "        def index():\n",
      "            return 'Hello World'\n",
      "    \n",
      "    For more information refer to :ref:`url-route-registrations`.\n",
      "    \n",
      "    :param rule: the URL rule as string\n",
      "    :param endpoint: the endpoint for the registered URL rule.  Flask\n",
      "                     itself assumes the name of the view function as\n",
      "                     endpoint\n",
      "    :param options: the options to be forwarded to the underlying\n",
      "                    :class:`~werkzeug.routing.Rule` object.  A change\n",
      "                    to Werkzeug is handling of method options.  methods\n",
      "                    is a list of methods this rule should be limited\n",
      "                    to (``GET``, ``POST`` etc.).  By default a rule\n",
      "                    just listens for ``GET`` (and implicitly ``HEAD``).\n",
      "                    Starting with Flask 0.6, ``OPTIONS`` is implicitly\n",
      "                    added and handled by the standard request handling.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(app.route)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c1ef927",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app \"__main__\" (lazy loading)\n",
      " * Environment: production\n",
      "\u001b[31m   WARNING: This is a development server. Do not use it in a production deployment.\u001b[0m\n",
      "\u001b[2m   Use a production WSGI server instead.\u001b[0m\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [23/Dec/2021 20:49:41] \"GET / HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [23/Dec/2021 20:49:47] \"GET /index HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [23/Dec/2021 20:49:49] \"POST /result HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [23/Dec/2021 21:17:30] \"GET / HTTP/1.1\" 200 -\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask,request\n",
    "from flask import render_template\n",
    "\n",
    "\n",
    "app = Flask(__name__)\n",
    "\n",
    "#1. 【GET】\n",
    "@app.route(\"/\",methods = ['GET'])\n",
    "def hello_world():\n",
    "    return render_template('hello.html')\n",
    "\n",
    "@app.route(\"/index\",methods=['GET'])\n",
    "def index():\n",
    "    return render_template('index.html')\n",
    "#2. 【POST】\n",
    "@app.route(\"/result\",methods=['POST'])\n",
    "def result():\n",
    "    #总过程：HTML 获取用户数据 --->python获取HTML数据--->处理数据--->传给HTML，做样式，提升用户视觉体验\n",
    "    # 思考：form的数据提交过来，用request.form接收？\n",
    "    py_firstname = request.form['firstname']\n",
    "    py_lastname = request.form['lastname']\n",
    "    # 对其数据进行处理操作\n",
    "    py_fullname = py_firstname.title() + ' ' + py_lastname.title()\n",
    "    # 将py_fullname 传给指定的html\n",
    "    return render_template('result.html',\n",
    "                          fullname = py_fullname)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f502ab26",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efd8acb9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
